package top.pmwly.CSRF.hadler;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName MyAuthenicationSuccessHandler.java
 * @Description 自定义的成功处理器
 * @createTime 2021年08月18日 14:26:00
 */
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private String url;

    public MyAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        User user = (User)authentication.getPrincipal();
        System.out.println(user.toString());
        //重定向到指定页面
        response.sendRedirect(url);
    }
}
